<?php


namespace App\Api\Controllers;


use App\Api\Manages\CommonController;
use App\Api\Repositories\GoodsCartRepositories;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class CartlistController extends CommonController
{
    //查询购物车列表
    public function cartList(Request $request)
    {
        $validat = $request->validate([
            'uid' => ['required'],
        ]);
        //根据用户ID获取购物车数据
        $data = GoodsCartRepositories::getCartAll((int)$validat['uid']);
        if($data){
            return JsonService::success('查询成功',$data,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('数据为空','',config('code.HTTP_NULL_DATA'));
        }
    }

    //购物车总价计算
    public function cartTotalPrice(Request $request)
    {
        //$cart=[[1,1],[1,2],[2,3]];
        $validat = $request->validate([
            'cart' => ['required'],
        ]);
        $cart = $this->arrayByStr($validat['cart']);
        $total = GoodsCartRepositories::getCartTotal($cart);
        if(is_numeric($total)){
            return JsonService::success('查询成功',$total,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('获得失败','',config('code.HTTP_FAIL'));
        }
    }

}